/* ======================================  路由  ====================================== */
import Type from '../fn/data-type'
const Index = {
	setParam(param,encode){
		let str='';
		for(let i in param){
			let value = param[i]
			if(encode) { value = encodeURIComponent(value)}
			if(Type.isValid(value)){ str += i + '=' +value + '&' }
		}
		if (str.charAt(str.length - 1) === '&'){ str = str.slice(0, str.length - 1) }
		str = '?' + str
		return str;	// encodeURI 不对 [:, /, ;,?] 进行编码
	},
	setPush(url, param, encode){
		url = param ? url + this.setParam(param,encode) : url
		return { 
			url,
			success:()=>{},
			fail:()=>{}
		}
	},
	push(url, param, encode){ wx.navigateTo(this.setPush(url, param, encode)) },
	replace(url, param){ wx.redirectTo(this.setPush(url, param)) },
	go(url, param){ wx.reLaunch(this.setPush(url, param)) },
	back(option){
		const { delta, refresh, param } = option || {}
		wx.navigateBack({ 
			delta,
			success: e =>{
				if(refresh){
					const page = getCurrentPages().pop();
					if (page == undefined || page == null) return;
					page.onRefresh(param);
				}
			}
		 } )
	 },
	pushTab(url){ wx.switchTab({url}) },
}
export default Index
